import threading
import time

# 自定义生产者线程类（补充实现，假设往列表 x 里添加元素）
class Producer(threading.Thread):
    def __init__(self, threadname):
        threading.Thread.__init__(self, name=threadname)

    def run(self):
        while True:
            time.sleep(1)  # 生产间隔，可调整
            con.acquire()
            x.append("item")  # 往共享列表 x 里添加元素，模拟生产
            print(f"Produced, x={x}")
            con.notify()  # 通知等待的消费者
            con.release()

# 自定义消费者线程类
class Consumer(threading.Thread):
    def __init__(self, threadname):
        threading.Thread.__init__(self, name=threadname)

    def run(self):
        while True:
            time.sleep(3)
            con.acquire()  # 获取锁
            if not x:
                print('Consumer is waiting.....')  # 等待
                con.wait()
            else:
                t = x.pop(0)
                print(f'Consumed:{t},x={x}')
                con.notify()
            con.release()

# 创建 Condition 对象以及生产者线程和消费者线程
con = threading.Condition()
x = []
p = Producer('Producer')
c = Consumer('Consumer')
p.start()
c.start()